﻿@model Logistics.Web.Models.Shipment.ShipmentDetailsModel

@using (Html.BeginForm())
{
    <div class="instruction-display">
        <h3>@T("Logistics.Order.Comment"):</h3>
        @if (Model.ShowDetails)
        {
            if (ViewBag.Comment == null || String.IsNullOrEmpty(ViewBag.Comment))
            {
                <p>None.</p>
            }
            else
            {
                <p>@ViewBag.Comment</p>
            }
        }
        else
        {
            <p>You are not authorized to view shipping instruction(s) for this package.</p>
        }

    </div>
    if (Model.ShowDetails && Model.CanChangeComment)
    {
        <div class="instruction-form">
            <h3>@T("Logistics.Order.AddComment"):</h3>
            @Html.TextAreaFor(model => model.Comment, new { @class = "form-control" })
            <input type="button" name="save" class="btn btn-normal form-control" value="@T("Admin.Common.Add")" id="logistics-shipment-package-comment" />
            <span class="red" id="packagecomment-result"></span>
        </div>
    }

    <script type="text/javascript">
        $(document).ready(function () {
            $(".packagecomment-result").hide();
            $("#logistics-shipment-package-comment").click(function () {
                var comment = $("#@Html.FieldIdFor(model => model.Comment)").val();
                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "@Url.RouteUrl("PackageComment")",
                    data: { "comment": comment, "packageId": @Model.PackageId },
                    success: function (response) {
                        if (response.Error) {
                            $("#packagecomment-result").html(response.Error);
                            $(".packagecomment-result").show();
                        }
                        else
                        {
                            location.reload();
                        }
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        $("#packagecomment-result").html(xhr.statusText);
                        $(".packagecomment-result").show();
                    }
                });
            });
        });
    </script>
}
